<template>
  <div>
    <div class="tab">
      <div class="header">
        <h4>设备管理</h4>
        <el-button type="success" size="small" @click="add">新增 + </el-button>
      </div>
      <hr />
      <div class="table">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="deviceId" label="设备ID" align="center"> </el-table-column>
          <el-table-column prop="cameraIndexCode" label="单兵ID" align="center"> </el-table-column>
          <el-table-column prop="address" label="操作" align="center">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleclick(scope.row, 1)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleclick(scope.row, 2)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page">
        <el-pagination background layout="prev, pager, next" :page-count="total" @current-change="change">
        </el-pagination>
      </div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form ref="form" :model="postmessage" label-width="80px" :rules="rules">
          <el-form-item label="单兵ID:" prop="cameraIndexCode">
            <el-input v-model="postmessage.cameraIndexCode" style="width: 85%"></el-input>
          </el-form-item>
          <el-form-item label="设备ID:" prop="deviceId">
            <el-input v-model="postmessage.deviceId" style="width: 85%"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import superadmin from '@/api/superadmin'
export default {
  data() {
    return {
      status: 1,
      dialogVisible: false,
      total: 0,
      pageNo: 1,
      pageSize: 7,
      tableData: [],
      postmessage: {
        cameraIndexCode: '',
        deviceId: '',
        id: '',
      },
      rules: {
        cameraIndexCode: [{ required: true, message: '请输入单兵ID号码', trigger: 'blur' }],
        deviceId: [{ required: true, message: '请输入设备ID号码', trigger: 'blur' }],
      },
    }
  },
  methods: {
    getpage() {
      superadmin.deviceList(this.pageNo, this.pageSize).then(e => {
        this.tableData = e.data.records
        this.total = e.data.pages
      })
    },
    add() {
      this.dialogVisible = true
      this.status = 1
    },
    handleclick(scope, val) {
      switch (val) {
        case 1:
          this.postmessage.cameraIndexCode = scope.cameraIndexCode
          this.postmessage.deviceId = scope.deviceId
          this.postmessage.id = scope.id
          this.dialogVisible = true
          this.status = 2
          break
        case 2:
          this.$confirm('确定删除该条设备信息?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              superadmin.deletesoldier(scope.id).then(e => {
                if (e.code == 200) {
                  this.$message({
                    type: 'success',
                    message: '删除成功!',
                  })
                  this.getpage()
                }
              })
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除',
              })
            })
          break
        default:
          break
      }
    },
    change(val) {
      this.pageNo = val
      this.getpage()
    },
    submit() {
      switch (this.status) {
        case 1:
          this.$refs.form.validate(valid => {
            if (valid) {
              superadmin.saveorupdate(this.postmessage).then(e => {
                if (e.code == 200) {
                  this.$message({
                    message: '操作成功！',
                    type: 'success',
                  })
                  this.getpage()
                  this.dialogVisible = false
                }
              })
            }
          })
          break
        case 2:
          this.$refs.form.validate(valid => {
            if (valid) {
              superadmin.saveorupdate(this.postmessage).then(e => {
                if (e.code == 200) {
                  this.$message({
                    message: '操作成功！',
                    type: 'success',
                  })
                  this.getpage()
                  this.dialogVisible = false
                }
              })
            }
          })
          break
        default:
          break
      }
    },
    close() {
      ;(this.postmessage = {
        cameraIndexCode: '',
        deviceId: '',
        id: '',
      }),
        (this.dialogVisible = false)
    },
    handleClose(done) {
      ;(this.postmessage = {
        cameraIndexCode: '',
        deviceId: '',
        id: '',
      }),
        done()
    },
  },
  created() {
    this.getpage()
  },
}
</script>

<style scoped lang="scss">
@import './device.scss';
</style>
